//
//  UserRootController.m
//  Water
//
//  Created by chenguibang on 2017/11/12.
//  Copyright © 2017年 chenguibang. All rights reserved.
//

#import "UserRootController.h"
#import "UserTableHeaderView.h"
#import "PlanController.h"
#import "YTLoginController.h"
#import "UserSettingController.h"
#import <UserCenterManager.h>
#import "EnvironmentController.h"
#import "BottleSettingController.h"
#import "CalculateVC.h"
#import <BlocksKit/BlocksKit+UIKit.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import <GBUserPhoneLoginController.h>

@interface UserRootController (){
    UserTableHeaderView *headerView;
}
@end

@implementation UserRootController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     headerView = WaterXIB(@"UserTableHeaderView");
    self.tableView.tableHeaderView = headerView;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [self.viewModel.refresh subscribeNext:^(id  _Nullable x) {
        [self.tableView reloadData];
    }];
    [self.viewModel setDefultMenus];
    
    [headerView.loginBtn bk_whenTapped:^{
        [self.tabBarController presentViewController:[[UINavigationController alloc] initWithRootViewController:[[GBUserPhoneLoginController alloc] initWithNibName:@"GBUserPhoneLoginController" bundle:nil]] animated:YES completion:^{
            
        }];
    }];
    [self setupView];
    
    [[UserCenterManager shared] addObserver:self forKeyPath:@"currentUser" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    
    [[UserCenterManager shared] getCurrentUserInfoWith:^(UserInfo *userInfo) {
        
        
    }];
   
}

- (void)login{
    [headerView.headerImageView sd_setImageWithURL:[NSURL URLWithString:[UserCenterManager shared].currentUser.avatar] placeholderImage:[UIImage imageNamed:@"header"]];
}

- (void)setupView{
    UIBarButtonItem *setBtn = [[UIBarButtonItem alloc] bk_initWithImage:UIImageWithNamed(@"setting") style:UIBarButtonItemStyleDone handler:^(id sender) {
        UserSettingController *userSettingController = [UserSettingController new];
        userSettingController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:userSettingController animated:YES];
    }];
    self.navigationItem.rightBarButtonItem = setBtn;
}
#pragma mark - TableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.viewModel.menus.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.viewModel.menus[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    MenuItem *item = self.viewModel.menus[indexPath.section][indexPath.row];
    cell.textLabel.text = item.title;
    cell.imageView.image = [UIImage imageNamed:item.imageName];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MenuItem *item = self.viewModel.menus[indexPath.section][indexPath.row];
    if ([item.title isEqualToString:@"我的计划"]) {
        PlanController *plan = [[PlanController alloc]init];
        plan.title = item.title;
        plan.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:plan animated:YES];
    }else if ([item.title isEqualToString:@"环境切换"]) {
        EnvironmentController *plan = [[EnvironmentController alloc]init];
        plan.hidesBottomBarWhenPushed = YES;
        plan.title = item.title;
        [self.navigationController pushViewController:plan animated:YES];
    }else if ([item.title isEqualToString:@"水杯设置"]) {
        BottleSettingController *plan = [[BottleSettingController alloc]initWithNibName:@"BottleSettingController" bundle:nil];
        plan.hidesBottomBarWhenPushed = YES;
        plan.title = item.title;
        [self.navigationController pushViewController:plan animated:YES];
    }else if ([item.title isEqualToString:@"水量计算"]) {
        CalculateVC *calculateVC =  [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CalculateVC"];
        calculateVC.hidesBottomBarWhenPushed = YES;
        calculateVC.title = item.title;
        [self.navigationController pushViewController:calculateVC animated:YES];
    }
    
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - GET SET

- (UserRootViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[UserRootViewModel alloc]init];
    }
    return _viewModel;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    [self login];
    headerView.loginBtn.hidden = [UserCenterManager shared].currentToken.length > 0;
    headerView.nickName.hidden = !headerView.loginBtn.hidden;
    headerView.nickName.text = [UserCenterManager shared].currentUser.nickname;
}







/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
